/**
 */
;
(function(){
'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningScreenCtrl', ['$scope','$modal','$stateParams',
	                                     'WarningAddService','BaseURL','Modal',
	    function ($scope,$modal,$stateParams,WarningAddService,baseUrl,Modal) {
				
		$scope.serv=WarningAddService;
		//监听warnScreenList
		$scope.$watch("warnScreenList",function(v){
			$scope.serv.warnScreenList=v;
		});
		$scope.checkRows = [];
		if(WarningAddService.warnScreenList){
			$scope.warnScreenList = WarningAddService.warnScreenList;
		}else{
			$scope.warnScreenList = [];
		}
		$scope.addScreen = function() {
			//打开弹出框
			var modalInstance = $modal.open({
	      	      templateUrl: baseUrl + 'views/hcpms/warning/warnEdit/warningAddScreenDialog_view.html',
	      	      backdrop:'static',
				  size:'sm',
	      	      controller:'WarningAddScreenCtrl'
		      	});
			 //接收返回数据
			 modalInstance.result.then(function(item) {
				if (!$scope.warnScreenList) {
					$scope.warnScreenList = [];
				}
				$scope.warnScreenList.push(item); 
	  	    });
		}
		
		//删除操作
		$scope.deleteScreen = function() {
			Modal.confirm('是否要删除已选择的行!' ,function(){
				
			});
			if(!$scope.warnScreenList){
				return;
			}
			
//			var warnList = [];
			angular.forEach($scope.warnScreenList,function(data,index){
			   if(data.checked === true){
				   $scope.warnScreenList.splice(index,1);
//				   warnList.push(data);
			   }
		   });
//		  $scope.warnScreenList = warnList;
		}

		
		//全选或者不全选
		$scope.checked = false;
		$scope.getWarnScreenIdList = function (checked){
			angular.forEach($scope.warnScreenList,function(data,index){
				data.checked = checked;
			});
		}
	}]).controller('addScreenCtrl', ['$scope','$modalInstance','Modal','WarningAddService',
	   function($scope,$modalInstance,Modal,WarningAddService){
		  //封装数据
	   	  $scope.warnscreen = {};
	   	  $scope.warnMonitorList=[];
	   	  $scope.warnRuleList=[]
	   	  function init(){
	    	  WarningAddService.getWarnMonitorList().success(function(response){
	    		  $scope.warnMonitorList =response;
	    	  });
	    	  WarningAddService.getWarnRuleList().success(function(response){
	    		  $scope.warnRuleList=response;
	    	  });
	       }
	   	  init();
	   	  $scope.changeMonitor = function(){
	   		  if($scope.warnscreen.monitor){
	   			  $scope.warnscreen.monitorNo=$scope.warnscreen.monitor.split('|')[0];
	   		  }else{
	   			  $scope.warnscreen.monitorNo=null;
	   		  }
	   	  }
	   	  $scope.changeRule = function(){
	   		if($scope.warnscreen.rule){
	   			$scope.warnscreen.ruleNo=$scope.warnscreen.rule.split('|')[0];
	   		}else{
	   			$scope.warnscreen.ruleNo=null;
	   		}
	   	  }
    	  //保存
	   	  $scope.confirm = function (){
	   		  var monitorNo=null;
	   		  var monitorType=null;
    		 if(!$scope.warnscreen.monitor){
    			 Modal.alert("监控类型不能为空!");
    			 return;
    		 } else{
    			 monitorNo=$scope.warnscreen.monitor.split('|')[0];
    			 monitorType=$scope.warnscreen.monitor.split('|')[1];
    		 }
    		 var ruleNo=null;
    		 var ruleName=null
    		 if(!$scope.warnscreen.rule){
    			 Modal.alert("规则名称不能为空!");
    			 return;
    		 }else{
    			 ruleNo=$scope.warnscreen.rule.split('|')[0];
    			 ruleName=$scope.warnscreen.rule.split('|')[1];
    		 }
    		 if(!$scope.warnscreen.warnValue){
    			 Modal.alert("阀值不能为空!");
    			 return;
    		 }
    		 if(!$scope.warnscreen.remark){
    			 Modal.alert("备注不能为空!");
    			 return;
    		 }
    		 var param = {
    			 monitorType:monitorType,
    			 monitorNo:monitorNo,
    			 ruleName:ruleName,
    			 ruleNo:ruleNo,
    			 warnValue:$scope.warnscreen.warnValue,
    			 remark:$scope.warnscreen.remark
    		 };
		     $modalInstance.close(param);
    	  }  
    	  //取消或或者直接关闭
    	  $scope.onOk = function(){
  		   $modalInstance.dismiss();
  	    }	
	}]);
})();